//
//  FDMScrollView.m
//  轮播图封装
//
//  Created by dllo on 16/4/12.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMScrollView.h"
#import "FDMPushWheelModel.h"

@interface FDMScrollView ()

@property (nonatomic, retain) NSMutableArray *picArr;

@end
@implementation FDMScrollView

#pragma mark - Createting And Init
- (instancetype)initWithFrame:(CGRect)frame withImages:(NSArray<UIImage *> *)images delegate:(id)delegate {
    self = [super initWithFrame:frame];
    if (self) {

        [self configWithFrame:frame WithImages:images delegate:delegate];
        
    }
    return self;
}

#pragma mark - config
- (void)configWithFrame:(CGRect)ScrollViewFrame WithImages:(NSArray *)images delegate:(id)delegate {

    // 设置contentSize
    self.contentSize = CGSizeMake((images.count + 2) * ScrollViewFrame.size.width, ScrollViewFrame.size.height);
    self.contentOffset = CGPointMake(ScrollViewFrame.size.width, 0);
    
    self.picArr = [NSMutableArray array];
    for (NSDictionary *dic in images) {
        FDMPushWheelModel *model = [[FDMPushWheelModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.picArr addObject:model];
    }
    for (NSInteger i = 0; i < images.count + 2; i++) {
        
        // 创建小的ScrollView
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(ScrollViewFrame.size.width * i, 0 , ScrollViewFrame.size.width, ScrollViewFrame.size.height)];
        scroll.backgroundColor = [UIColor purpleColor];
        [self addSubview:scroll];
       
        
        if (i == 0) {
             FDMPushWheelModel *model = self.picArr[images.count - 1];
            // 创建imageView子视图
            UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            imageButton.frame = CGRectMake(0, 0, ScrollViewFrame.size.width, ScrollViewFrame.size.height);
            imageButton.backgroundColor = [UIColor yellowColor];
            [scroll addSubview:imageButton];
            [imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.pic] forState:UIControlStateNormal placeholderImage:PLACEHOLDER_IMAGE];
        }
        if (i > 0 && i < images.count + 1) {
            FDMPushWheelModel *model = self.picArr[i - 1];
            // 创建imageView子视图
            UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            imageButton.frame = CGRectMake(0, 0, ScrollViewFrame.size.width, ScrollViewFrame.size.height);
            imageButton.backgroundColor = [UIColor yellowColor];
            [scroll addSubview:imageButton];
            [imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.pic] forState:UIControlStateNormal placeholderImage:PLACEHOLDER_IMAGE];
        }
        if (i == images.count + 1) {
            FDMPushWheelModel *model = self.picArr[0];
            // 创建imageView子视图
            UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            imageButton.frame = CGRectMake(0, 0, ScrollViewFrame.size.width, ScrollViewFrame.size.height);
            imageButton.backgroundColor = [UIColor yellowColor];
            [scroll addSubview:imageButton];
            [imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.pic] forState:UIControlStateNormal placeholderImage:PLACEHOLDER_IMAGE];
        }
        
        // 缩放比例
        scroll.minimumZoomScale = 0.5;
        scroll.maximumZoomScale = 2;
        scroll.delegate = delegate;
    }
    
    // 按页滚动
    self.pagingEnabled = YES;
    
    // 边缘回弹效果
    self.bounces = NO;
    
    // 滚动条
//    self.showsHorizontalScrollIndicator = NO;
}

@end
